# -*- coding: UTF-8 -*-
from PIL import Image
plus = lambda x: x.zfill(8)
mod = lambda x,y: x % y
toDec = lambda x: int(str(x),2)

def get_key(strs):
    str_ = ""
    for i in range(len(strs)):
        str_ = str_+plus(bin(ord(strs[i])).replace('0b',''))
    return str_

im = Image.open('./0.png')
im = im.convert("RGB")
keys = get_key(open('./flag.txt','r').read())
keylen = len(keys)
width = im.width
height = im.height
count = 0
for h in range(height):
    for w in range(width):
        pixel = im.getpixel((w,h))
        # print("源:{0}".format(pixel))
        r,g,b = pixel[0],pixel[1],pixel[2]
        if count == keylen:
            break
        r_b,g_b,b_b = plus(bin(r).replace('0b',"")),plus(bin(g).replace('0b',"")),plus(bin(b).replace('0b',""))
        print("{0}\n{1}\n{2}\n".format(r_b,g_b,b_b))
        # r_b[-1] = keys[count]
        r_b = list(r_b)
        r_b[-1] = keys[count]
        r_b = ''.join(r_b)
        count += 1
        g_b = list(g_b)
        g_b[-1] = keys[count]
        g_b = ''.join(g_b)        
        count += 1
        if count == keylen:
            im.putpixel((w,h),(toDec(r_b),toDec(g_b),toDec(b_b)))
            break
        b_b = list(b_b)
        b_b[-1] = keys[count]
        b_b = ''.join(b_b)        
        count += 1
        print("{0}\n{1}\n{2}\n".format(r_b,g_b,b_b))
        if count % 3 == 0:
            im.putpixel((w,h),(toDec(r_b),toDec(g_b),toDec(b_b)))

im.save('./pic.png')
